package com.example.site_management_p1.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SiteRabbitConfig {
        public static final String QUEUE_NORMAL = "queue-site-money";
        public static final String EXCHANGE_NAME="site-exchange";
        public static final String ROUTINGKEY_SITE="SITE-KEY";
        public static final String DEAD_EXCHANGE_NAME="DEAD-EXCHANGE-SITE";
        public static final String DEAD_QUEUE_NAME="DEAD-QUEUE-SITE";
        public static final String DEAD_ROUTINGKEY_SITE="DEAD-SITE-KEY";
        //声明交换机
        @Bean(EXCHANGE_NAME)
        public Exchange exchange(){
            return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
        }

        @Bean(QUEUE_NORMAL)
        public Queue siteQueue(){
                return QueueBuilder.durable(QUEUE_NORMAL).ttl(60000*30).maxLength(10000)
                .deadLetterExchange(DEAD_ROUTINGKEY_SITE)
                .deadLetterRoutingKey(DEAD_ROUTINGKEY_SITE).build();
        }

        @Bean
        public Binding bindingSite(@Qualifier(QUEUE_NORMAL) Queue queue,
                                    @Qualifier(EXCHANGE_NAME) Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SITE).noargs();
        }

        @Bean(DEAD_EXCHANGE_NAME)
        public Exchange deadExchange(){
                return ExchangeBuilder.topicExchange(DEAD_ROUTINGKEY_SITE).durable(true).build();
        }
        @Bean(DEAD_QUEUE_NAME)
        public Queue deadQueue(){
                return QueueBuilder.durable(DEAD_QUEUE_NAME).build();
        }

        @Bean
        public Binding bindingSiteDead(@Qualifier(DEAD_QUEUE_NAME) Queue queue,
                                   @Qualifier(DEAD_EXCHANGE_NAME) Exchange exchange){
                return BindingBuilder.bind(queue).to(exchange).with(DEAD_ROUTINGKEY_SITE).noargs();
        }
}
